﻿@{
    ViewBag.Title = "MaterialClass";
    Layout = "~/Areas/IM/Views/Shared/_Layout.cshtml";
}
@section Scripts{
    <script type="text/javascript">
        window.ondragstart = function () { window.event.returnValue = false; }
        window.oncontextmenu = function () { window.event.returnValue = false; }
        window.onselectstart = function () { event.returnValue = false; }
        var ParentCode;
        var ParentName;
        var LowerCode;
        var LowerName;
        $(document).ready(function () {
            initTree();
            $.setDivOnly("#TableForm");
        });
        $(window).resize(function () {
            $.setDivOnly("#TableForm");
        });
        function initTree() {
            $('#MaterialTree').tree({
                url: '@Url.Content("~/IM/AllDictionary/GetAllMaterialTypeByUserInfo")',
                method: 'POST',
                animate: true,
                lines: true,
                onContextMenu: function (e, node) {
                    e.preventDefault();
                    // 查找节点
                    $('#MaterialTree').tree('select', node.target);
                    var pnode = $('#MaterialTree').tree('getParent', node.target);
                    if (pnode != null) {
                        ParentCode = pnode.id;
                        ParentName = pnode.text;
                        LowerCode = node.id;
                        LowerName = node.text;
                        // 显示快捷菜单
                        $('#menu').menu('show', {
                            left: e.pageX,
                            top: e.pageY
                        });
                    }
                },
                formatter: function (node) {
                    if (node.attributes.isAticon == false) {
                        return '<span style="color:red">' + node.text + '</span>';
                    } else {
                        return node.text;
                    }
                }
            });
        }
        function AddMaterialLevel() {
            $.GID("btnadd").style.display = "block";
            $.GID("btnedit").style.display = "none";
            $('#ParentName').textbox('setText', ParentName);
            $('#ParentID').val(ParentCode)
            $('#MaterialType').textbox('clear');
            $('#MaterialEdit').dialog('open');
        }
        function AddMaterialLowerLevel() {
            $.GID("btnadd").style.display = "block";
            $.GID("btnedit").style.display = "none";
            $('#ParentName').textbox('setText', LowerName);
            $('#ParentID').val(LowerCode);
            $('#MaterialType').textbox('clear');
            $('#MaterialEdit').dialog('open');
        }
        function EditMaterialInfo() {
            $.GID("btnadd").style.display = "none";
            $.GID("btnedit").style.display = "block";
            $('#ParentName').textbox('setText', ParentName);
            $('#MaterialTypeID').val(LowerCode);
            $('#MaterialType').textbox('setText', LowerName);
            $('#MaterialEdit').dialog('open');
        }
        function submitAddForm() {
            var isValid = $("#MaterialEdit").form("validate");
            if (isValid) {
                $('#btnadd').linkbutton('disable')
                $.ajax({
                    type: 'POST',
                    url: '@Url.Content("~/IM/MaterialList/SaveMaterialType")',
                    data:
                        {
                            PID: $('#ParentID').val(),
                            MTypeName: $('#MaterialType').textbox('getText')
                        },
                    dataType: "json",
                    success: function (jsData) {
                        $.handleJsonResult(jsData, function () {
                            initTree()
                            $('#MaterialEdit').window('close');
                        }, function () {
                            $('#btnadd').linkbutton('enable');
                        });
                    }
                });
            }
        }
        function submitEditForm() {
            $('#btnedit').linkbutton('disable')
            $.ajax({
                type: 'POST',
                url: '@Url.Content("~/IM/MaterialList/EditMaterialType")',
                data:
                    {
                        MID: $('#MaterialTypeID').val(),
                        MTypeName: $('#MaterialType').textbox('getText')
                    },
                dataType: "json",
                success: function (jsData) {
                    $.handleJsonResult(jsData, function () {
                        initTree()
                        $('#MaterialEdit').window('close');
                    }, function () {
                        $('#btnedit').linkbutton('enable');
                    });
                }
            });
        }
        function DisableMaterialType() {
            $.messager.confirm('提示确认', '确认将[物资类型：【' + LowerName + '】禁用吗？', function (r) {
                if (r) {
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Content("~/IM/MaterialList/DisableMaterialType")',
                        data:
                            {
                                MID: LowerCode
                            },
                        dataType: "json",
                        success: function (jsData) {
                            $.handleJsonResult(jsData, function () {
                                initTree();
                            });
                        }
                    });
                }
            });
        }
        function EnableMaterialType() {
            $.ajax({
                type: 'POST',
                url: '@Url.Content("~/IM/MaterialList/EnableMaterialType")',
                data:
                    {
                        MID: LowerCode
                    },
                dataType: "json",
                success: function (jsData) {
                    $.handleJsonResult(jsData, function () {
                        initTree();
                    });
                }
            });
        }

    </script>
}

<div id="TableForm" class="easyui-layout" style="width:auto; height: auto; margin-left: auto; margin-right: auto">
    <div data-options="region:'center',title:'物资分类管理',iconCls:'icon-custom-cGoodClass'">
        <ul id="MaterialTree"></ul>
        <div id="menu" class="easyui-menu" style="width:150px;">
            <div data-options="iconCls:'icon-custom-cGoodClass'" onclick="AddMaterialLevel()">添加平级物资分类</div>
            <div data-options="iconCls:'icon-add'" onclick="AddMaterialLowerLevel()">添加下级物资分类</div>
            <div class="menu-sep"></div>
            <div data-options="iconCls:'icon-edit'" onclick="EditMaterialInfo()">修改物资分类信息</div>
            <div class="menu-sep"></div>
            <div data-options="iconCls:'icon-custom-cJinYong'"><span style="color:red">禁用</span>该物资分类</div>
            <div class="menu-sep"></div>
            <div data-options="iconCls:'icon-custom-cQiYong'"><span style="color:darkgreen">启用</span>该物资分类</div>
        </div>
    </div>
</div>

<div id="MaterialEdit" class="easyui-dialog" title="维护物资分类" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:false,iconCls:'icon-custom-cGoodClass'" style="width:300px;height:155px;padding:0px;">
    <div data-options="region:'center'" style="overflow: hidden">
        <table style="width:99%">
            <tr style="height:40px">
                <td>上级物资：</td>
                <td>
                    <input id="ParentName" class="easyui-textbox" style="width:160px" data-options="readonly:true" />
                    <input id="ParentID" type="hidden" />
                </td>
            </tr>
            <tr style="height:40px">
                <td>物资名称：</td>
                <td>
                    <input id="MaterialType" class="easyui-textbox" style="width:160px" data-options="required:true" />
                    <input id="MaterialTypeID" type="hidden" />
                </td>
            </tr>
            <tr style="height:30px">
                <td colspan="2">
                    <a href="javascript:void(0)" id="btnedit" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="submitEditForm();">确认修改物资</a>
                    <a href="javascript:void(0)" id="btnadd" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="submitAddForm();">确认新增物资</a>
                </td>
            </tr>
        </table>
    </div>
</div>